
/*单链表（含头结点）*/
#include<stdio.h>
#include<stdlib.h>
 

typedef int ElemType;
 
typedef struct LNode{
	ElemType data;  //数据域
	struct LNode *next;  // 指针域
}LNode,*LinkList;
LinkList CreateList2(ElemType a[], int n); //尾插法创建单链表
void OutputList(LinkList L);
 
 
void main(){
	LinkList L = NULL;
	ElemType a[] = { 1, 2, 3, 4, 5 };
	ElemType e;
	L = CreateList2(a, 5);
	OutputList(L);
 
}


LinkList CreateList2(ElemType a[], int n){   //尾插法创建单链表
	int i;
	LinkList L = NULL, s = NULL, r = NULL;
	L = (LNode*)malloc(sizeof(LNode));
	L->next = NULL;
	r = L;
	for (i = 0; i < n; i++){
		s = (LNode*)malloc(sizeof(LNode));
		s->data = a[i];
		s->next = NULL;
		r->next = s;
		r = s;  //r指向新的表尾结点
	}
	r->next = NULL;
	return L;
}
void OutputList(LinkList L){  //输出单链表全部元素
	LinkList p = L->next;
	while (p){
		printf("%d  ", p->data);
		p = p->next;
	}
	printf("\n");
}
